﻿using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using JSTree.Sample.data;
using JSTree.Sample.Models;

namespace JSTree.Sample.Controllers
{
    public class HomeController : Controller
    {
        NodesRepository repository = new NodesRepository();

        public ActionResult Index()
        {
            var nodes = repository.GetNodes();
            return View(nodes);
        }

        [HttpPost]
        public ActionResult Rename(Node node)
        {
            repository.SetNameById(node.Id, node.Title);
            return null;
        }

        [HttpPost]
        public ActionResult Remove(Node node)
        {
            repository.RemoveById(node.Id);
            return null;
        }

        [HttpPost]
        public ActionResult MoveNode(Node node)
        {
            repository.Move(node);
            return null;
        }

        [HttpPost]
        public JsonResult AddNode(Node node)
        {
            repository.AddNode(node);
            var id = repository.GetIdBy(node);
            return Json(id, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult GetChildren(int id)
        {
            var children = repository.GetChildrenNodes(id);
            return Json(children, JsonRequestBehavior.AllowGet);
        }
    }
}
